package org.vacoor.xqq.util;

import javax.swing.*;
import java.lang.management.*;
import java.util.List;

/**
 * User: vacoor
 */
public class JVM {

    public final static int JDK1_0 = 1000;
    public final static int JDK1_1 = 1100;
    public final static int JDK1_2 = 1200;
    public final static int JDK1_3 = 1300;
    public final static int JDK1_4 = 1400;
    public final static int JDK1_5 = 1500;
    public final static int JDK1_6 = 1600;
    public final static int JDK1_6N = 1610;
    public final static int JDK1_7 = 1700;

    private static JVM current;
    private static boolean is32Arch;
    private static boolean is64Arch;

    static {
        current = new JVM();
    }

    /**
     * @return the current JVM object
     */
    public static JVM current() {
        return current;
    }

    private int jdkVersion;

    /**
     * Creates a new JVM data from the <code>java.version</code>
     * System property
     */
    public JVM() {
        this(System.getProperty("java.version"));
    }

    /**
     * Constructor for the OS object
     */
    public JVM(String p_JavaVersion) {
        if (p_JavaVersion.startsWith("1.7.")) {
            jdkVersion = JDK1_7;
        } else if (p_JavaVersion.startsWith("1.6.")) {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel".equals(info.getClassName())) {
                    jdkVersion = JDK1_6N;
                    break;
                }
            }

            jdkVersion = jdkVersion == 0 ? JDK1_6 : jdkVersion;
        } else if (p_JavaVersion.startsWith("1.5.")) {
            jdkVersion = JDK1_5;
        } else if (p_JavaVersion.startsWith("1.4.")) {
            jdkVersion = JDK1_4;
        } else if (p_JavaVersion.startsWith("1.3.")) {
            jdkVersion = JDK1_3;
        } else if (p_JavaVersion.startsWith("1.2.")) {
            jdkVersion = JDK1_2;
        } else if (p_JavaVersion.startsWith("1.1.")) {
            jdkVersion = JDK1_1;
        } else if (p_JavaVersion.startsWith("1.0.")) {
            jdkVersion = JDK1_0;
        } else {
            // unknown version, assume 1.3
            jdkVersion = JDK1_3;
        }
    }

    public boolean isOrLater(int p_Version) {
        return jdkVersion >= p_Version;
    }

    public boolean isOneDotOne() {
        return jdkVersion == JDK1_1;
    }

    public boolean isOneDotTwo() {
        return jdkVersion == JDK1_2;
    }

    public boolean isOneDotThree() {
        return jdkVersion == JDK1_3;
    }

    public boolean isOneDotFour() {
        return jdkVersion == JDK1_4;
    }

    public boolean isOneDotFive() {
        return jdkVersion == JDK1_5;
    }

    public boolean isOneDotSix() {
        return jdkVersion == JDK1_6 || isOneDotSixUpdateN();
    }

    /**
     * Determines if the version of JDK1_6 has Nimbus Look and Feel installed.
     *
     * @return {@code true} if Nimbus is available and the version is 1.6;
     *         {@code false} otherwise
     */
    public boolean isOneDotSixUpdateN() {
        return jdkVersion == JDK1_6N;
    }

    public boolean isOneDotSeven() {
        return jdkVersion == JDK1_7;
    }

    public static void main(String[] args) {
        RuntimeMXBean rtMBean = ManagementFactory.getRuntimeMXBean();
        /** {@link sun.management.VMManagementImpl#getVmId()} 可以看到 */
        /*
          public String getVmId() {
            int i = getProcessId();
            String str = "localhost";
            try {
              str = InetAddress.getLocalHost().getHostName();
            }
            catch (UnknownHostException localUnknownHostException)
            {
            }
            return i + "@" + str;
          }

          private native int getProcessId();

          VmName 等也是从System.getProperty() 取得的
         */
        System.out.println(rtMBean.getName()); // PID@Host
        System.out.println(rtMBean.getSpecName());
        System.out.println(rtMBean.getSpecVendor());
        System.out.println(rtMBean.getVmVersion());
        System.out.println(rtMBean.getInputArguments());

        OperatingSystemMXBean osMBean = ManagementFactory.getOperatingSystemMXBean();
        // win server 2008
        System.out.println(osMBean.getName());  // Windows Server 2008 R2
        System.out.println(osMBean.getArch());  //amd64
        System.out.println(osMBean.getVersion()); // 6.1 win 2008 为 NT 6.1
        System.out.println(osMBean.getAvailableProcessors());   // 4
        System.out.println(osMBean.getSystemLoadAverage()); // -1.0

        MemoryMXBean memMBean = ManagementFactory.getMemoryMXBean();
        System.out.println(memMBean.getHeapMemoryUsage());
        System.out.println(memMBean.getNonHeapMemoryUsage());
        System.out.println(memMBean.getObjectPendingFinalizationCount());
        System.out.println(System.getenv("userprofile"));

        List<GarbageCollectorMXBean> gcMBeans = ManagementFactory.getGarbageCollectorMXBeans();
        for(GarbageCollectorMXBean gcMBean : gcMBeans) {
//            System.out.println(gcMBean.get);
        }
    }
}
